home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_481 / wavemaker / guts.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  351 lines

  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <exec/types.h>
  4. #include <exec/memory.h>
  5. #include <devices/audio.h>
  6. #include <proto/intuition.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfx.h>
  9.  
  10. extern struct IntuitionBase *IntuitionBase;
  11. extern struct GfxBase *GfxBase;
  12. extern struct Window *Window;
  13. extern struct Screen *Screen;
  14. extern struct ViewPort *ViewPort;
  15. extern struct RastPort *rport;
  16. extern struct IOAudio *ioa,*audio,*finish[4],*audchan[4];
  17. extern struct IntuiText About1;
  18. extern struct Gadget propor8;
  19. extern struct Gadget propor7;
  20. extern struct Gadget propor6;
  21. extern struct Gadget propor5;
  22. extern struct Gadget propor4;
  23. extern struct Gadget propor3;
  24. extern struct Gadget propor2;
  25. extern struct Gadget propor1;
  26. extern struct IntuiText textamp;
  27. extern struct IntuiText OK;
  28. extern struct IntuiText Answer1;
  29. extern struct Border RWave_brd5;
  30. extern struct Border UWave_brd5;
  31. extern struct Border SWave_brd5;
  32.  
  33. extern SHORT sinewave[8][513];
  34. extern BYTE *usersound,*refsound;
  35. extern char textbuf[6];
  36. extern LONG amps[8],refamps[8];
  37. extern USHORT userwave[513][2],refwave[513][2],selwave[513][2];
  38. extern UBYTE selflag;
  39. extern SHORT waiting[5];
  40.  
  41. #define YES     1
  42. #define NO      0
  43.  
  44. extern LONG maxuser,maxref;
  45. extern LONG maxamp;
  46. extern BYTE waveform[514],refform[514],selform[514];
  47. extern BYTE *usersound,*refsound;
  48. extern SHORT gameon;
  49. extern char Answers[80];
  50.  
  51. /********************************************************************/
  52. VOID Update(USHORT gadno)
  53.  
  54. /*  Update the amplitudes and screen when one amplitude has been changed */
  55.  
  56. {
  57.     VOID AmpsUpdate(),DrawUser();
  58.     LONG MakeWave();
  59.     
  60.     if (amps[gadno]>127) amps[gadno]=127;
  61.     if (amps[gadno]<-127) amps[gadno]=-127;
  62.     maxuser=MakeWave(amps,waveform,usersound);
  63.     AmpsUpdate();
  64.     DrawUser();
  65. }
  66.  
  67. /*************************************************************/
  68. VOID DoMenu(USHORT mcode)
  69.  
  70. /*  Does the right thing when a menu option is chosen */
  71.  
  72. {
  73. #define PROJECT_MENU 0
  74. #define WAVE_MENU    1
  75. #define GAME_MENU    2
  76.  
  77. #define ABOUT        0
  78. #define FULLSCALE    1
  79. #define SCREENBACK   2
  80. #define SCREENFRONT  3
  81. #define QUIT         4
  82.  
  83. #define START_EASY   0
  84. #define START_HARD   1
  85. #define STOP         2
  86.  
  87. switch(MENUNUM(mcode))
  88. {
  89.     LONG peak,retval;
  90.     SHORT i,item,scale;
  91.     LONG temp,AddWaves(),randamp(),checkval(),MakeWave(),AutoRequest();
  92.     VOID AmpsUpdate(),PropUpdate(),DrawUser(),WaveCleanup(),rescale();
  93.     static SHORT canned[4][8]={{  0,  0,  0,  0,  0,  0,  0,  0},
  94.                                {108,  0,-12,  0,  4,  0, -2,  0},
  95.                            { 74,-38, 24,-20, 14,-12, 10, -9},
  96.                            {124,  0, 41,  0, 25,  0, 18,  0}};
  97.  
  98.                    
  99.     
  100.     case PROJECT_MENU:
  101.         switch (ITEMNUM(mcode))
  102.         {
  103.             case ABOUT:
  104.             {
  105.                 retval=AutoRequest(Window,&About1,&OK,&OK,
  106.                    NULL,NULL,500,130);
  107.                 break;
  108.             }
  109.             case FULLSCALE:
  110.             {
  111.                     if ((peak=checkval(maxuser,amps))!=0) 
  112.                    rescale(peak,amps);
  113.                 maxuser=AddWaves(amps,0xFF,waveform,
  114.                     usersound)>>15;
  115.                 AmpsUpdate();
  116.                 PropUpdate();
  117.                 DrawUser();
  118.                 break;
  119.             }
  120.             case SCREENBACK:
  121.             {
  122.                 ScreenToBack(Screen);
  123.                 break;
  124.             }
  125.             case SCREENFRONT:
  126.             {
  127.                 ScreenToFront(Screen);
  128.                 break;
  129.             }
  130.             case QUIT:
  131.             {
  132.                 WaveCleanup();
  133.                 Exit(TRUE);
  134.             }
  135.         }
  136.         break;
  137.     case WAVE_MENU:
  138.         item = ITEMNUM(mcode);
  139.         for (i=0; i<8; ++i)
  140.         {
  141.             amps[i] = canned[item][i];
  142.         }
  143.                 if (item == 0)
  144.                 {
  145.                     for (i=0; i<4; i++)
  146.                     {
  147.                         BeginIO(finish[i]);
  148.                         WaitIO(finish[i]);
  149.                         waiting[i]=NO;
  150.                     }
  151.                 }
  152.         AmpsUpdate();
  153.         PropUpdate();
  154.         maxuser=MakeWave(amps,waveform,usersound);
  155.         DrawUser();
  156.         break;
  157.     case GAME_MENU:
  158.         switch (ITEMNUM(mcode))
  159.         {
  160.             case START_EASY:
  161.                 gameon = YES;
  162.                 for (i=0; i<4; i++)
  163.                 {
  164.                     refamps[i] = randamp();
  165.                 }
  166.                 for (i=4;i<8;i++) refamps[i]=0;
  167.                 maxref=MakeWave(refamps,refform,refsound);
  168.                     if (maxref==0) maxref=1;
  169.                 scale=maxamp/maxref;
  170.                 for (i=0;i<4;++i)
  171.                 {
  172.                     temp=refamps[i]*scale;
  173.                     refamps[i]=temp>>7;
  174.                 }
  175.                 maxref=AddWaves(refamps,0x0F,refform,
  176.                     NULL)>>15;
  177.                 for (i=0; i<513; ++i)
  178.                 {
  179.                     refwave[i][1]=((129-refform[i])*48)>>7;
  180.                 }
  181.                 DrawUser();
  182.                 break;
  183.             case START_HARD:
  184.                 gameon = YES;
  185.                 for (i=0; i<8; i++)
  186.                 {
  187.                     refamps[i] = randamp();
  188.                 }
  189.                 maxref=MakeWave(refamps,refform,refsound);
  190.                     if (maxref==0) maxref=1;
  191.                 scale=maxamp/maxref;
  192.                 for (i=0;i<8;++i)
  193.                 {
  194.                     temp=refamps[i]*scale;
  195.                     refamps[i]=temp>>7;
  196.                 }
  197.                 maxref=AddWaves(refamps,0xFF,refform,
  198.                     NULL)>>15;
  199.                 for (i=0; i<513; ++i)
  200.                 {
  201.                     refwave[i][1]=((129-refform[i])*48)>>7;
  202.                 }
  203.                 DrawUser();
  204.                 break;
  205.             case STOP:
  206.                 gameon = NO;
  207.                 sprintf(Answers,
  208.                  "%6d %6d %6d %6d %6d %6d %6d %6d",
  209.                  refamps[0],refamps[1],refamps[2],
  210.                  refamps[3],refamps[4],refamps[5],
  211.                  refamps[6],refamps[7]);
  212.                 retval=AutoRequest(Window,&Answer1,&OK,&OK,
  213.                    NULL,NULL,620,80);
  214.                 DrawUser();
  215.                 break;
  216.         }
  217.         break;
  218. }
  219. }
  220.  
  221. /****************************************************************/
  222. VOID AmpsUpdate()
  223.  
  224. /*  Update the numerical amplitude displays   */
  225.  
  226. {
  227.     SHORT i;
  228.     SHORT boxx[8]={34,108,182,256,330,404,478,552};
  229.     
  230.     for (i=0;i<8;i++)
  231.     {
  232.         sprintf(textbuf,"%5d",amps[i]);
  233.         PrintIText(rport,&textamp,boxx[i],188);
  234.     }
  235. }
  236.  
  237. /****************************************************************/
  238. VOID PropUpdate()
  239.  
  240. /*  Update the proportional gadgets after a rescaling  */
  241.  
  242. {
  243.     USHORT temp;
  244.     temp=amptopot(amps[7]);
  245.     ModifyProp(&propor8,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  246.         temp,0x0800,0x0800);
  247.     temp=amptopot(amps[6]);
  248.     ModifyProp(&propor7,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  249.         temp,0x0800,0x0800);
  250.     temp=amptopot(amps[5]);
  251.     ModifyProp(&propor6,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  252.         temp,0x0800,0x0800);
  253.     temp=amptopot(amps[4]);
  254.     ModifyProp(&propor5,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  255.         temp,0x0800,0x0800);
  256.     temp=amptopot(amps[3]);
  257.     ModifyProp(&propor4,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  258.         temp,0x0800,0x0800);
  259.     temp=amptopot(amps[2]);
  260.     ModifyProp(&propor3,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  261.         temp,0x0800,0x0800);
  262.     temp=amptopot(amps[1]);
  263.     ModifyProp(&propor2,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  264.         temp,0x0800,0x0800);
  265.     temp=amptopot(amps[0]);
  266.     ModifyProp(&propor1,Window,NULL,FREEVERT|AUTOKNOB,0xFFFF,
  267.         temp,0x0800,0x0800);
  268. }
  269.  
  270. /*****************************************************************/
  271. LONG MakeWave(LONG ampl[],BYTE wf[],UBYTE *ws)
  272.  
  273. /*  Makes the compound waveform (via AddWaves) and checks that it */
  274. /*  is not off scale.  If it is, it rescales.                     */
  275. {
  276.     LONG peak;
  277.     LONG maxnow;
  278.     LONG AddWaves();
  279.     LONG checkval();
  280.     VOID rescale();
  281.     
  282.     maxnow=AddWaves(ampl,0xFF,wf,ws)>>15;
  283.     if ((peak=checkval(maxnow,ampl))>127)
  284.     {
  285.         rescale(peak,ampl);
  286.         maxnow=AddWaves(ampl,0xFF,wf,ws)>>15;
  287.                 PropUpdate();
  288.     }
  289.     return(maxnow);
  290. }
  291.  
  292. /******************************************************************/
  293. LONG checkval(LONG maxval,LONG ampl[])
  294.  
  295. /*  Checks maxval, and all of the amps[i] to find the largest     */
  296. /*  in absolute value.                                            */
  297.  
  298. {
  299.     SHORT i;
  300.     LONG temp;
  301.     
  302.     temp = maxval;
  303.     for (i=0; i<8; ++i)
  304.     {
  305.         if (abs(ampl[i])>temp) temp=abs(ampl[i]);
  306.     }
  307.     return(temp);
  308. }
  309.  
  310. /******************************************************************/
  311. VOID rescale(LONG peak,LONG ampl[])
  312.  
  313. /*  rescale to make the amplitude a high as possible without having */
  314. /*  any ampltiude or the compound wave exceed 127 in absolute value */
  315. {
  316.     extern LONG maxamp;
  317.     SHORT scale,i;
  318.     LONG temp;
  319.     
  320.     scale=maxamp/peak;
  321.     for (i=0;i<8;++i)         /* Be careful not to lose bits */
  322.     {
  323.         temp=ampl[i]*scale;
  324.         ampl[i]=temp>>7;
  325.     }
  326. }
  327.  
  328. /***********************************************************************/
  329. VOID DrawUser()
  330.  
  331. /*  Draws the compound waveform in the display area.  */
  332.  
  333. {
  334.     SHORT i;
  335.         LONG maxsel;
  336.  
  337.     for (i=0;i<513;++i) userwave[i][1]=((129-waveform[i])*48)>>7;
  338.     SetAPen(rport,1);
  339.     RectFill(rport,64,14,577,109);
  340.     SetAPen(rport,3);
  341.     RectFill(rport,64,61,577,61);
  342.         if (selflag)
  343.         {
  344.           maxsel=AddWaves(amps,selflag,selform,NULL);
  345.           for (i=0; i<513; i++) selwave[i][1]=((129-selform[i])*48)>>7;
  346.           DrawBorder(rport,&SWave_brd5,0,0);
  347.         }
  348.     if (gameon) DrawBorder(rport,&RWave_brd5,0,0);
  349.     DrawBorder(rport,&UWave_brd5,0,0);
  350. }
  351.